public class Solution {
    public static void main(String[] args) {
        Solution s = new Solution();
        int[] nums = new int[]{2,0,2,1,1,0};
        s.sortColors(nums);
        for(int e: nums) {
            System.out.print(e + " ");
        }
    }

    public void sortColors(int[] nums) {
        /**
         * 颜色分类-三指针*/
        // 1 预处理
        int n = nums.length;

        // 2 划分
        int left = -1;
        int right = n;
        for(int i = 0; i < n; i++) {
            if(i == right) {
                break;
            }
            if(nums[i] == 0) {
                swap(nums, i, ++left);
            }
            if(nums[i] == 2) {
                swap(nums, i, --right);
            }
        }
    }

    public static void swap(int[] arr, int a, int b) {
        /**
         * 交换元素*/
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
}
